package com.wlz.server;

import com.wlz.common.Meassage;
import com.wlz.common.MeassageType;
import com.wlz.utils.HessianSerializerUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 源码学院-ant
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
public class SocketHandler implements Runnable {
    private Socket socket;
    private String userName;

    public SocketHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) > 0) {
                Meassage requestMeassage = HessianSerializerUtil.deserialize(buffer);
                if (MeassageType.LOGIN.name().equals(requestMeassage.getType())) {
                    //保存握手连接
                    System.out.println("用户:"+requestMeassage.getContext()+" 连接成功");
                    setUserName(requestMeassage.getContext());
                    HandshakeContainer.clientMap.put(requestMeassage.getContext(), socket);
                    out = socket.getOutputStream();
                    out.write(HessianSerializerUtil.serialize(new Meassage(MeassageType.OK.name(), "连接成功")));
                }
                if (MeassageType.MEASSAGE.name().equals(requestMeassage.getType())) {
                    //这里取的socket是当前连接用户想要发送消息目标用户的socket对象
                    Socket socket = HandshakeContainer.clientMap.get(requestMeassage.getToUser());
                    if(socket!=null){
                        System.out.println("用户:<"+userName+"> 发送消息给用户<"+requestMeassage.getToUser()+">");
                        out = socket.getOutputStream();
                        out.write(HessianSerializerUtil.serialize(
                                new Meassage(MeassageType.MEASSAGE.name(),
                                        requestMeassage.getContext())));
                    }else{
                        out = socket.getOutputStream();
                        out.write(HessianSerializerUtil.serialize(new Meassage(MeassageType.FAIL.name(), "用户不存在")));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
